package lk.icta.core.service.impl;

import lk.icta.common.ELandRuntimeException;
import lk.icta.core.dao.CalenderScheduleDAO;
import lk.icta.core.entity.CalenderSchedule;
import lk.icta.core.entity.CalenderScheduleType;
import lk.icta.core.service.CalenderScheduleManagementService;
import lk.icta.user.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Kusal Samarakoon
 */
public class CalenderScheduleManagementServiceImpl implements CalenderScheduleManagementService {

    private static final Logger logger = LoggerFactory.getLogger(CalenderScheduleManagementServiceImpl.class);
    private final CalenderScheduleDAO calenderScheduleDAO;

    public CalenderScheduleManagementServiceImpl(CalenderScheduleDAO calenderScheduleDAO) {
        this.calenderScheduleDAO = calenderScheduleDAO;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void addCalenderSchedule(CalenderSchedule schedule, User user) {
        logger.debug("Adding a Calender Schedule {}", schedule.getCalenderId());
        /*if (!user.isAuthorized(Permission.LAND_VISIT_SCHEDULE)) {
            handleELandRuntimeException(user.getFullName() + " doesn't have permission to add a calender schedule", ErrorCodes.UNAUTHORIZED_ACCESS);
        } else {
            calenderScheduleDAO.addCalenderSchedule(schedule, user);
        }*/


        calenderScheduleDAO.addCalenderSchedule(schedule, user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateCalenderSchedule(CalenderSchedule schedule, User user) {
        logger.debug("Updating a Calender Schedule {}", schedule.getCalenderId());
        /*if(!user.isAuthorized(Permission.LAND_VISIT_SCHEDULE)){
            handleELandRuntimeException(user.getFullName() + " doesn't have permission to update a calender schedule", ErrorCodes.UNAUTHORIZED_ACCESS);
        }else{
            calenderScheduleDAO.updateCalenderSchedule(schedule, user);
        }*/
        calenderScheduleDAO.updateCalenderSchedule(schedule, user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteCalenderSchedule(String id) {
        logger.debug("Deliting a Calender Schedule {}", id);
        calenderScheduleDAO.deleteCalenderSchedule(id);
    }


    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public CalenderSchedule getCalenderScheduleById(String id, User user) {
        logger.debug("getCalenderScheduleById {}", id);
        return calenderScheduleDAO.getCalenderScheduleById(id, user);
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public List<CalenderSchedule> getAllCalenderSchedule(User user) {
        logger.debug("getAllCalenderSchedule {}", user);
        return calenderScheduleDAO.getAllCalenderSchedule(user);

    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public List<CalenderSchedule> getAllCalenderScheduleByUser(User user) {
        logger.debug("getAllCalenderSchedule by user {}", user);
        return calenderScheduleDAO.getAllCalenderScheduleByUser(user);

    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public List<CalenderSchedule> getAllCalenderScheduleByCalenderScheduleType(CalenderScheduleType scheduleType, User user) {
        logger.debug("getAllCalenderSchedule by CalenderScheduleType user {}", user);
        return calenderScheduleDAO.getAllCalenderScheduleByCalenderScheduleType(scheduleType, user);

    }

    private void handleELandRuntimeException(String message, int code) {
        logger.error(message);
        throw new ELandRuntimeException(message, code);
    }

}
